﻿
//***********************************************************************
// * Module:   TbBstFraudOperationhistoryHandler
// * 作者:  曹荣芝
// * Modified: 2014/5/26
// * Purpose: Definition of the Class TbBstFraudOperationhistoryHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using System.Collections;
using XWG.Framework;
using JWBSTAPP.CommonClass;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// TbBstFraudOperationhistoryHandler 的摘要说明
    /// </summary>
    public class TbBstFraudOperationhistoryHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {

                TbBstFraudOperationhistory mTbBstFraudOperationhistory = new TbBstFraudOperationhistory();
                mTbBstFraudOperationhistory.Id = Guid.NewGuid().ToString(); //主键
                mTbBstFraudOperationhistory.Operationtype = context.Request.Form["texOperationtype"]; // 0 下载  1 上传
                mTbBstFraudOperationhistory.Filepath = context.Request.Form["texFilepath"]; //下载或上传原始文件路径
                mTbBstFraudOperationhistory.Records = Convert.ToDecimal(context.Request.Form["texRecords"]); //上传或下载的数据的条数
                mTbBstFraudOperationhistory.Username = context.Request.Form["texUsername"]; //操作的用户名  
                mTbBstFraudOperationhistory.Operatorsname = context.Request.Form["texOperatorsname"]; //运营商 移动 联通 电信
                mTbBstFraudOperationhistory.Operatorscode = context.Request.Form["texOperatorscode"]; //1移动 2联通 3电信
                mTbBstFraudOperationhistory.Remark = context.Request.Form["texRemark"]; //备注

                mResultModel = TbBstFraudOperationhistoryBLL.Insert(mTbBstFraudOperationhistory, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                TbBstFraudOperationhistory mTbBstFraudOperationhistory = new TbBstFraudOperationhistory();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = TbBstFraudOperationhistoryBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion

        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            TbBstFraudOperationhistory mTbBstFraudOperationhistory = new TbBstFraudOperationhistory();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mTbBstFraudOperationhistory = TbBstFraudOperationhistoryBLL.Load(paramKey); //根据键值获取实体信息
                if (mTbBstFraudOperationhistory != null)
                {
                    mTbBstFraudOperationhistory.Operationtype = context.Request.Form["texOperationtype"]; // 0 下载  1 上传
                    mTbBstFraudOperationhistory.Filepath = context.Request.Form["texFilepath"]; //下载或上传原始文件路径
                    mTbBstFraudOperationhistory.Records = Convert.ToDecimal(context.Request.Form["texRecords"]); //上传或下载的数据的条数
                    mTbBstFraudOperationhistory.Username = context.Request.Form["texUsername"]; //操作的用户名  
                    mTbBstFraudOperationhistory.Operatorsname = context.Request.Form["texOperatorsname"]; //运营商 移动 联通 电信
                    mTbBstFraudOperationhistory.Operatorscode = context.Request.Form["texOperatorscode"]; //1移动 2联通 3电信
                    mTbBstFraudOperationhistory.Remark = context.Request.Form["texRemark"]; //备注

                    mResultModel = TbBstFraudOperationhistoryBLL.Update(mTbBstFraudOperationhistory, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            Hashtable ht = new Hashtable();
            string paramKey = context.Request.Params["id"];
            string json = "";
            string dmt = "";
            try
            {
                TbBstFraudOperationhistory mTbBstFraudOperationhistory = new TbBstFraudOperationhistory(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mTbBstFraudOperationhistory = TbBstFraudOperationhistoryBLL.Load(paramKey); //根据键值获取管理员
                    if (mTbBstFraudOperationhistory != null)
                    {
                        json = JsonConvert.SerializeObject(mTbBstFraudOperationhistory);
                    }
                    if (!string.IsNullOrEmpty(mTbBstFraudOperationhistory.Operationtype))
                    {
                        string isMov = mTbBstFraudOperationhistory.Operationtype == "0" ? "下载操作" : "上传操作";
                        ht.Add("Isext", isMov);
                    }
                    if (mTbBstFraudOperationhistory.Createdate.ToString("yyyy") == "0001")
                    {
                        dmt = "暂无操作时间";
                        ht.Add("ShowTime", dmt);
                    }
                    else
                    {
                        dmt = mTbBstFraudOperationhistory.Createdate.ToString();
                        ht.Add("ShowTime", dmt);
                    }
                    json = JsonString.AddAttribute(json, ht);
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            string texUser = context.Request.Form["texUser"];
            string selOPERATORS = context.Request.Form["selOPERATORS"];
            string selType = context.Request.Form["selType"];
            DateTime begintime = new DateTime();
            DateTime endtime = new DateTime();
            if (!string.IsNullOrEmpty(context.Request.Form["texTimeStart"]))
            {
                begintime = Convert.ToDateTime(context.Request.Form["texTimeStart"]);
            }
            if (!string.IsNullOrEmpty(context.Request.Form["texTimeEnd"]))
            {
                endtime = Convert.ToDateTime(context.Request.Form["texTimeEnd"]);
            }
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtTbBstFraudOperationhistory = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage Q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();
            SqlConditionList sqllist = new SqlConditionList();
            if (!string.IsNullOrEmpty(texUser))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstFraudOperationhistoryConst.Columns.Username, '%' + texUser + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(selOPERATORS))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstFraudOperationhistoryConst.Columns.Operatorscode, selOPERATORS), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(selType))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstFraudOperationhistoryConst.Columns.Operationtype, selType), SqlLogicOper.AND_LOGIC);
            }
            if (begintime.ToString() != "0001/1/1 0:00:00")
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_LESS_THAN, TbBstFraudOperationhistoryConst.Columns.Createdate, begintime), SqlLogicOper.AND_LOGIC);
            }
            if (endtime.ToString() != "0001/1/1 0:00:00")
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_MORE_THAN, TbBstFraudOperationhistoryConst.Columns.Createdate, endtime), SqlLogicOper.AND_LOGIC);
            }
            dtTbBstFraudOperationhistory = Q.GetRecordByPage("*", sqllist.ToSql(), "CREATEDATE", intPageSize, intPageIndex, TbBstFraudOperationhistoryConst.TableName, TbBstFraudOperationhistoryConst.FieldKey, "").Tables[0];
            if (dtTbBstFraudOperationhistory != null && dtTbBstFraudOperationhistory.Rows.Count > 0)
            {
                for (int i = 0; i < dtTbBstFraudOperationhistory.Rows.Count; i++)
                {
                    if (dtTbBstFraudOperationhistory.Rows[i]["operationtype"].ToString() == "0")
                    {
                        dtTbBstFraudOperationhistory.Rows[i]["operationtype"] = "数据下载";
                    }
                    else
                    {
                        dtTbBstFraudOperationhistory.Rows[i]["operationtype"] = "数据上传";
                    }
                }
                for (int i = 0; i < dtTbBstFraudOperationhistory.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtTbBstFraudOperationhistory.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtTbBstFraudOperationhistory;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            string json = JsonConvert.SerializeObject(tm, dtConverter);            
            context.Response.Write(json);
        }
        #endregion

    }
}


